﻿@{
    ViewBag.Title = "Upload Files";
}
@using (Html.BeginForm("Upload", "Import", FormMethod.Post, new { id = "formUpload", enctype = "multipart/form-data" }))
{
    <fieldset class="fields">
        <br />
        <div>
            Type:
            <select name="fileType" id="fileType">
                <option>-</option>
                <option value="Functions">Functions</option>
                <option value="ManagedSegment">Managed Segment</option>
                <option value="DSMT">DSMT</option>
                <option value="GOCCodes">GOCs Codes</option>
            </select>
            <br />
            <br />
            File:
            <input type="file" class="file" id="file" name="file" style="margin-left: 09px" />
        </div>
        <br />
        <input type="button" id="importFile" value="Import" title="Import" />
    </fieldset>    
    
}
<script type="text/javascript">

    $(document).ready(function () {
        $('#importFile').click(importFile);
        $('#formUpload').ajaxForm({
            dataType: 'post',
            success: function (text) {
                var json = $.parseJSON(text);
                $("#loadingMessage").html(json.customMessage);
                $("#formUpload").trigger('reset');
            }
        });
    });

    function importFile(e) {
        e.preventDefault();

        var regExp = new RegExp('^.*\.(xls|XLS|xlsx|XLSX)$');
        var fileName = $('#file').val();
        var fileType = $('#fileType').val();
        var message = '';

        if (fileType == '-') {
            message = '<center><br/><img src="@Url.Content("~/Content/Images/exclamation.png")" alt="Exclamation"/><br/><br/>Please, select valid type of file!</center>';
        }

        if (message == '' && fileName == '') {
            message = '<center><br/><img src="@Url.Content("~/Content/Images/exclamation.png")" alt="Exclamation"/><br/><br/>Please, select a file!</center>';
        }

        if (message == '' && fileName.match(regExp) == null) {
            message = '<center><br/><img src="@Url.Content("~/Content/Images/exclamation.png")" alt="Exclamation"/><br/><br/>Please, select a valid file!<center>';
        }

        $("#loading").dialog('open');
        if (message != '') {
            $("#loadingMessage").html(message);
            return;
        }

        $("#loadingMessage").html('<center><br/><img src="@Url.Content("~/Content/Images/Loading.gif")" alt="Loading"/><br/><br/>Please, waiting.<br/>Do not close this window.</center>');

        $('#formUpload').submit();
    }


</script>
